﻿#region Using namespaces...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using TaxonomyImportLibrary.Taxonomy.Views;
using TaxonomyImportLibrary.Taxonomy;
using System.IO;
using TaxonomyImportLibrary.Taxonomy.Interfaces;
#endregion

namespace TaxonomyImportTool
{
	/// <summary>
	/// MainForm class
	/// </summary>
	public partial class MainForm : Form
	{
		#region Enumerations
		/// <summary>
		/// 
		/// </summary>
		private enum TreeImageTypes
		{
			/// <summary>
			/// TermGroup enumeration member
			/// </summary>
			TermStore = 0,
			/// <summary>
			/// TermGroup enumeration member
			/// </summary>
			TermGroup = 1,
			/// <summary>
			/// TermSet enumeration member
			/// </summary>
			TermSet = 2,
			/// <summary>
			/// Term enumeration member
			/// </summary>
			Term = 3
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets Manager
		/// </summary>
		private TaxonomyXmlImportManager Manager
		{
			get
			{
				return TaxonomyXmlImportManager.Instance;
			}
		}
		#endregion

		#region Constructors
		/// <summary>
		/// Default constructor
		/// </summary>
		public MainForm()
		{
			InitializeComponent();
		}
		#endregion

		#region Event Handlers
		/// <summary>
		/// Handles file open click events
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void _open_Click(object sender, EventArgs e)
		{
			// Start
			if (_openFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
			{
				// Clear nodes and properties
				_taxonomyTree.Nodes.Clear();
				_property.SelectedObject = null;
				
				string fileName = _openFileDialog.FileName;
				if (File.Exists(fileName))
				{
					// Load xml source
					Manager.LoadTaxonomyFromXml(fileName, XmlSourceTypes.FileSource);

					// Process data
					if (!(Manager.Definition == null))
					{
						// Load nodes
						TreeNode defaultTermStoreNode = new
							TreeNode("Default Term Store",
							(int)TreeImageTypes.TermStore,
							(int)TreeImageTypes.TermStore);

						// Load term groups
						foreach (IGroupView group in Manager.Definition.ChildGroups)
						{
							// Create group node
							TreeNode groupNode = new TreeNode(group.Name, (int)TreeImageTypes.TermGroup, (int)TreeImageTypes.TermGroup)
								{
									Tag = group
								};

							// Load term sets
							LoadTermSets(groupNode, group);

							// Add group node
							groupNode.Expand();
							defaultTermStoreNode.Nodes.Add(groupNode);
						}
						

						// Load nodes
						defaultTermStoreNode.Expand();
						_taxonomyTree.Nodes.Add(defaultTermStoreNode);

						// Enable deploy
						_deploy.Enabled = true;
					}
				}
			}
		}

		/// <summary>
		/// Handles node selection
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void _taxonomyTree_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (!(e.Node.Tag == null))
				_property.SelectedObject = e.Node.Tag;
		}

		/// <summary>
		/// Handles application exit click events
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void _exit_Click(object sender, EventArgs e)
		{
			// Quit
			if (MessageBox.Show(this, 
				"Are you sure you wish to exit?", 
				"SharePoint 2010 Taxonomy Import Utility", 
				MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
				Application.Exit();
		}

		/// <summary>
		/// Handles taxonomy deployment click events
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void _deploy_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show(this,
				"Are you sure you wish to deploy this taxonomy definition?",
				"SharePoint 2010 Taxonomy Import Utility",
				MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
			{
				try
				{
					// Set UI cues
					_deployingMessage.Text = string.Format("Deploying to the default term store located at: {0}. Please wait...", Manager.Definition.SiteUrl);
					this.Cursor = Cursors.WaitCursor;

					// Deploy
					Manager.BuildTermStore();

					// Set UI cues
					_deployingMessage.Text = "Deployment is complete";
					this.Cursor = Cursors.Arrow;

					// Disable deployment
					_deploy.Enabled = false;
				}
				catch(Exception ex)
				{
					// Set UI cues
					_deployingMessage.Text = ex.Message;
					this.Cursor = Cursors.Arrow;
				}
			}
		}
		#endregion

		#region Utility
		/// <summary>
		/// Loads term set nodes as children of the group node
		/// </summary>
		/// <param name="groupNode"></param>
		/// <param name="group"></param>
		private void LoadTermSets(TreeNode groupNode, IGroupView group)
		{
			// Process
			if (group.ChildTermSets.Count > 0)
			{
				foreach (ITermSetView termSetView in group.ChildTermSets)
				{
					TreeNode node = new TreeNode(termSetView.Name,
						(int)TreeImageTypes.TermSet,
						(int)TreeImageTypes.TermSet)
					{
						Tag = termSetView
					};

					// Load terms
					if (termSetView.ChildTerms.Count > 0)
					{
						foreach (ITermView termView in termSetView.ChildTerms)
						{
							TreeNode termNode = new TreeNode(termView.DefaultLabel,
								(int)TreeImageTypes.Term,
								(int)TreeImageTypes.Term)
							{
								Tag = termView
							};

							if (termView.ChildTerms.Count > 0)
								LoadChildTerms(termNode, termView.ChildTerms);

							// Load node
							node.Nodes.Add(termNode);
						}
					}

					// Load node
					groupNode.Nodes.Add(node);
				}
			}
		}

		/// <summary>
		/// Loads child terms recursively
		/// </summary>
		/// <param name="termNode"></param>
		/// <param name="list"></param>
		private void LoadChildTerms(TreeNode node, List<ITermView> list)
		{
			foreach (ITermView termDefinition in list)
			{
				TreeNode termNode = new TreeNode(termDefinition.DefaultLabel,
					(int)TreeImageTypes.Term, (int)TreeImageTypes.Term)
				{
					Tag = termDefinition
				};

				// Load children
				if (termDefinition.ChildTerms.Count > 0)
					LoadChildTerms(termNode, termDefinition.ChildTerms);

				// Load node
				node.Nodes.Add(termNode);
			}
		}
		#endregion
	}
}
